const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    goods_list: [],
    latitude: 30.595754,
    longitude: 114.278421,
    markers: [

    ]
  },

  get_shop_list() {
    app.request("/get_store_list", {}).then(res => {
      console.log(res, "店铺列表");
      let arr = res.data.data
      let shop_arr = arr.map(item => {
        return {
          id: item.id,
          latitude: item.lat,
          longitude: item.lng,
          // alpha:0.5,
          width: 50,
          height: 50,
          iconPath: "/static/map/1.png",
          width: 50,
          height: 50,
          iconPath: "/static/map/1.png",
          callout: {
            content: item.title,
            padding: 5,
            anchorY: 5,
            display: 'ALWAYS',
            textAlign: 'center',
            borderRadius: 10,
            borderColor: '#ffd00f',
            borderWidth: 1,
          }
        }
      })


      this.setData({
        markers: shop_arr
      })




    })
  },




  clickMarker(e) {
    let id = e.detail.markerId

    console.log(e);
    app.request("/get_storegoods", {
      data: {
        id
      }
    }).then(res => {
      console.log(res);
      this.setData({
        goods_list: res.data.data
      })
    })


    wx.showToast({
      title: "店铺：" + e.detail.markerId,
      icon: "none"
    })
  },



  get_my_location() {
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res);
        const latitude = res.latitude
        const longitude = res.longitude
        that.setData({
          latitude,
          longitude
        })
      }
    })
  },
  //获取附近八公里商家的商品信息
  latestshop() {
    var that = this
    app.request("/user/get_location_store", {
      data: {

      }
    }).then(res => {
      console.log(res, '--------------------------------')
      if (res.data.code == 200) {
        that.setData({
          goods_list: res.data.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    this.get_my_location()
    app.request("/user/update_latlng", {
      method: "POST",
      data: {
        latitude: that.data.latitude,
        longitude: that.data.longitude
      }
    }).then(res => {})
    that.latestshop() //返回距离用户八公里之内最近的商家商品信息
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log(res)
      },
      fail(e) {
        console.log(e)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.get_shop_list()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res, '88888888888888888888')
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面的对象
      var url = currentPage.route //当前页面url
      console.log(url, '888888888888')
      console.log("来自右上角转发菜单")
      return {
        title: '分享至朋友圈',
        path: url,
        success: (res) => {
          console.log("转发成功", res);
        },
        fail: (res) => {
          console.log("转发失败", res);
        }
      }
    }

  },
})